<!DOCTYPE html>
<!-- Created by 白墨 on 2021/4/17 22:27:31-->
<html lang="zh-cmn-Hans-CN" xmlns:th="http://www.thymeleaf.org">

<head th:replace="admin/_fragments::head(~{::title})">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>朋友们</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
  <link rel="stylesheet" href="../../static/css/me.css">
</head>

<body>
<!-- 导航 -->
<nav th:replace="admin/_fragments::menu(1)" class="ui inverted attached segment">

</nav>


<div class="ui attached pointing menu tm-hf">
  <div class="ui container">
    <div class="right menu" style="margin-right: 150px;">
      <a href="#" class="blue active item">列表</a>
    </div>
  </div>
</div>
<!-- 中间部分 -->
<div class="m-container m-padded-tb-big">
    <div class="ui secondary segment form tm">
      <div class="inline fields">
        <div class="field">
          <input type="text" name="name" placeholder="网站名/网站/站长">
        </div>
        <div class="field">
          <div class="ui selection dropdown">
            <input type="hidden" name="flag" value="-1">
            <i class="dropdown icon"></i>
            <div class="default text">状态</div>
            <div class="menu">
              <div class="item" data-value="-1">默认</div>
              <div class="item" data-value="1">启用中</div>
              <div class="item" data-value="2">申请中</div>
              <div class="item" data-value="3">禁用中</div>
            </div>
          </div>
        </div>
        <div class="field">
          <div class="ui selection dropdown">
            <input type="hidden" name="sort" value="0">
            <i class="dropdown icon"></i>
            <div class="default text">等级排序</div>
            <div class="menu">
              <div class="item" data-value="0">默认</div>
              <div class="item" data-value="-1">高级在前</div>
              <div class="item" data-value="-2">低级在前</div>
            </div>
          </div>
        </div>
        <div class="field">
          <div class="ui checkbox">
            <input type="checkbox" id="time" name="time">
            <label for="time">最新</label>
          </div>
        </div>
        <div class="field">
          <button type="button" id="search-btn" class="ui mini blue basic button"><i class="search icon"></i>搜索</button>
        </div>
      </div>
    </div>
    <div id="table-container">
      <div class="ui container form" th:classappend="${result!=null?(result?'success':'error'):''}">
      <div class="ui message" th:classappend="${result!=null?(result?'success':'error'):'success'}">
        <i class="close icon"></i>
        <div class="header">提示：</div>
        <p th:text="${message}">恭喜，操作成功！</p>
      </div>
      </div>
      <table th:fragment="friendList" class="ui compact blue table tm">
        <thead>
        <tr>
          <th></th>
          <th class="three wide">网站</th>
          <th class="two wide">站长</th>
          <th class="two wide">优先级</th>
          <th class="three wide">状态</th>
          <th class="three wide">发布时间</th>
          <th class="three wide">操作</th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="friend,iterStat : ${pageInfo.list}">
          <td th:text="${iterStat.count}">1</td>
          <td><a th:text="${friend.name}" th:href="${friend.url}">百度</a></td>
          <td th:text="${friend.nickname}">白墨</td>
          <td>
            <span th:text="${friend.sort}">1</span>
            <a href="#" onclick="sort(this)" th:attr="data-sort=${friend.sort+1},data-id=${friend.id},data-flag=${friend.flag}"><i class="long arrow alternate up icon"></i></a>
            <a href="#" onclick="sort(this)" th:attr="data-sort=${friend.sort-1},data-id=${friend.id},data-flag=${friend.flag}"><i class="long arrow alternate down icon"></i></a>
          </td>
          <td>
            <span th:if="${friend.flag==1}" style="color:green;">启用中</span>
            <span th:if="${friend.flag==2}" style="color:orange;">申请中</span>
            <span th:if="${friend.flag==3}" style="color:gray;">已禁用</span>
          </td>
          <td th:text="${#dates.format(friend.createTime,'yyyy-MM-dd HH:mm')}">2020-1-20 09:45</td>
          <td>
            <a href="#" onclick="update(this)"
               th:attr="data-sort=${friend.sort},data-id=${friend.id},data-flag=${friend.flag}"
               th:classappend="${friend.flag==1?'grey':(friend.flag==2?'green':'orange')}"
               th:text="${friend.flag==1?'禁用':(friend.flag==2?'通过':'启用')}"
               class="ui mini basic button">已启用</a>
            <a href="#" onclick="return confirm('确定要删除吗？')" th:href="@{/admin/friends/{id}/delete(id=${friend.id},pageNum=${pageNum})}" class="ui mini red basic button">删除</a>
          </td>
        </tr>
        </tbody>
        <tfoot>
        <tr>
          <th colspan="4">
            <div class="ui mini pagination menu tm-qt" th:if="${pageInfo.pages}>1">
              <a th:href="@{/admin/friends(pageNum=1)}" class=" item">首页</a>
              <a th:href="@{/admin/friends(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}" class="  item" th:if="${pageInfo.hasPreviousPage}">上一页</a>
              <a th:href="@{/admin/friends(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}" class=" item" th:if="${pageInfo.hasNextPage}">下一页</a>
              <a th:href="@{/admin/friends(pageNum=${pageInfo.pages})}" class=" item">尾页</a>
            </div>
          </th>
          <th colspan="3" class="right aligned">
            当前 <span th:text="${pageInfo.pageNum}"></span> 页,总 <span th:text="${pageInfo.pages}"></span> 页,共 <span th:text="${pageInfo.total}"></span> 个网站
          </th>
        </tr>
        </tfoot>
      </table>
  </div>
</div>

<br>
<br>

<!-- 底部 -->
<footer th:replace="admin/_fragments::footer" class="ui inverted vertical segment m-padded-tb-massive">

</footer>


<script src="https://lib.baomitu.com/jquery/3.5.1/jquery.js"></script>
<script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.js"></script>


<script>
  $('.menu.toggle').click(function() {
    $('.m-item').toggleClass('m-mobile-hide');
  });

  $('.ui.dropdown').dropdown({
    on: 'hover'
  });
  //消息提示关闭初始化
  $('.message .close').on('click', function () {
            $(this).closest('.message').transition('fade');
          });

  $('#clear-btn').on('click', function() {
            $('.ui.type.dropdown').dropdown('clear');
          });

  //搜索按钮
  $("#search-btn").click(function () {
    let email="";
    if($("[name='time']").prop('checked')){
      email="now";
    }
    $("#table-container").load("[[@{/admin/friends/search}]]",{
      "name" : $("[name='name']").val(),
      "flag" : $("[name='flag']").val(),
      "sort" : $("[name='sort']").val(),
      "email" : email
    });

  });

  //等级加减
  function sort(obj){
    let id = $(obj).data('id');
    let sort = $(obj).data('sort');
    let flag = $(obj).data('flag');
    let pageNum = "[[${pageInfo.pageNum}]]";
    if (sort<=0){
      alert("再降就掉地底啦！")
      return false;
    }
    $.post("[[@{/admin/friends}]]",{
      "id":id,
      "sort":sort,
      "flag":flag,
      "pageNum":pageNum
    })
    // $.ajax({
    //   url:'[[@{/admin/friends}]]',	//请求地址
    //   type:'POST',				//Post请求
    //   data:{
    //     "id":id,
    //     "sort":sort,
    //     "flag":flag,
    //     "pageNum":pageNum
    //   }
    // })
    $("#table-container").load("[[@{/admin/friends}]]",{
      "id":id,
      "sort":sort,
      "flag":flag,
      "pageNum":pageNum
    });
  }

  //状态更新
  function update(obj){
    let id = $(obj).data('id');
    let sort = $(obj).data('sort');
    let flag = $(obj).data('flag');
    let pageNum = "[[${pageInfo.pageNum}]]";
    if (flag==1){
      if (!confirm("确定要禁用吗？")){
        return false;
      }
      flag=3;
    }else if (flag==2){
      if (!confirm("确定要通过申请吗？")){
        return false;
      }
      flag=1;
    }else if (flag==3){
      if (!confirm("确定要启用吗？")){
        return false;
      }
      flag=1;
    }
    $("#table-container").load("[[@{/admin/friends}]]",{
      "id":id,
      "sort":sort,
      "flag":flag,
      "pageNum":pageNum
    });
  }

  //页脚
  $('#newblog-container').load("[[@{/footer/newblog}]]");
</script>
</body>

</html>